package com.guoxue.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.guoxue.dao.IPlanProgressDao;
import com.guoxue.domain.PlanProgressVo;

@Repository
public class PlanProgressDaoImpl extends BaseDaoImpl<PlanProgressVo> implements IPlanProgressDao {

	@Override
	public List<PlanProgressVo> getUserPlans(int id) {
		return find("From PlanProgress p WHERE p.uid=?", new Object[] { id });
	}

	@Override
	public PlanProgressVo getPlanByUserAndPlanId(int id, int planId) {
		return get("From PlanProgressVo p WHERE p.uid=? AND p.currentPlanId=?", new Object[] { id, planId });
	}

	@Override
	public void savePlan(PlanProgressVo plan) {
		saveOrUpdate(plan);
	}

	@Override
	public List<PlanProgressVo> getUserPlansByPlanId(int id, int beginPlanId, int endPlanId) {
		return find("From PlanProgressVo p WHERE p.uid=? AND p.currentPlanId >=? AND p.currentPlanId<=?",
				new Object[] { id, beginPlanId, endPlanId });
	}

	@Override
	public int getReadersCount(int id, int begin, int end) {
		List<PlanProgressVo> li = find(
				"From PlanProgressVo p WHERE p.uid=? AND p.currentPlanId >=? AND p.currentPlanId<=? AND p.medal=0",
				new Object[] { id, begin, end });
		return li.size();
	}

	@Override
	public PlanProgressVo getUserPlanByUidAndPlanId(int id, int planId) {
		return get("From PlanProgressVo p WHERE p.uid=? AND p.currentPlanId=?",new Object[] { id,planId});
	}

	@Override
	public int getCount(int readRound) {
		List<PlanProgressVo> li = find(
				"From PlanProgressVo p WHERE p.currentReadRound=?",
				new Object[] { readRound});
		return li.size();
	}

	@Override
	public int getCompleteCount(int currentReadRound, int currentReadCount) {
		List<PlanProgressVo> li = find(
				"From PlanProgressVo p WHERE p.currentReadRound>=?",
				new Object[] { currentReadRound});
		return li.size();
	}

}
